package javaer.starter.placeholder.character;

import javaer.starter.constants.SymbolEnum;
import javaer.starter.placeholder.PlaceholderTokenParser;

import java.util.Set;

/**
 * @author yangzhen
 * @date 2021/12/7 15:54
 * @since 1.0.0
 */
public class ImportMappingTokenHandler extends PlaceholderTokenParser {

    private Set<String> qualifiedNames;

    private String basePackage;

    public ImportMappingTokenHandler(Set<String> qualifiedNames, String basePackage) {
        this.qualifiedNames = qualifiedNames;
        this.basePackage = basePackage;
    }

    @Override
    public String handleToken(String content) {
        content = dealImport(content);
        return getOpenToken() + content + getCloseToken();
    }

    @Override
    public String getOpenToken() {
        return SymbolEnum.IMPORT.getSymbol();
    }

    @Override
    public String getCloseToken() {
        return SymbolEnum.SEMICOLON.getSymbol();
    }

    private String dealImport(String content) {
        content = content.trim();
        if (qualifiedNames.contains(content)) {
            content = content.replace(basePackage, SymbolEnum.BASE_PACKAGE.getSymbol());
        }
        content = content.replace(basePackage, SymbolEnum.BASE_PACKAGE.getSymbol());
        return content;
    }
}
